class Bundle < ActiveRecord::Base
  has_and_belongs_to_many :publications
  has_many :application_accounts, :through => :bundle_variations
  has_many :bundle_variations, :dependent => :destroy
  has_many :trial_periods, :through => :bundle_variations
  
  validates_associated :bundle_variations
  validates_presence_of :name, :description
  validates_uniqueness_of :name
  
  accepts_nested_attributes_for :bundle_variations, :reject_if => proc { |attributes| attributes['frequency'].blank? }, :allow_destroy => true

  #----- Act as FINITE STATE MACHINE -------------
    acts_as_state_machine :initial => :active, :column => :status

    state :inactive, :column => :status
    state :active, :column => :status
    state :canceled, :column => :status

    event :activate do
       transitions :from => :inactive, :to => :active
    end

    event :deactivate do
      transitions :from => :active, :to => :inactive
    end
    
    event :cancel do
      transitions :from => :active, :to => :canceled
    end
    
    # reference of statuses for this model
    def self.statuses
      [RefActionStatus::ACTIVE, RefActionStatus::INACTIVE, RefActionStatus::CANCELED]
    end
  #-----------------------------------------------
  private

end
